package nxu.it.movieapi.service.pg;

import nxu.it.api.model.Cast;
import nxu.it.api.model.CastRole;
import nxu.it.api.model.Filmography;
import nxu.it.api.service.MovieCastService;
import nxu.it.movieapi.service.pg.dao.MovieCastPgDao;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MovieCastServicePgImpl implements MovieCastService {
    private final Jdbi jdbi;

    public MovieCastServicePgImpl(@Qualifier("createSecondJdbi") Jdbi jdbi) {
        this.jdbi = jdbi;
        // 确保安装了SqlObjectPlugin插件
        this.jdbi.installPlugin(new SqlObjectPlugin());
    }

    @Override
    public List<Cast> findCastByMovieId(Integer movieId) {
        // 使用Jdbi的withExtension方法来调用MovieCastPgDao接口中的方法
        return jdbi.withExtension(MovieCastPgDao.class, dao -> dao.findCastsByMovieId(movieId));
    }

    @Override
    public List<Cast> findCastByMovieIdAndRole(Integer movieId, CastRole role) {
        return jdbi.withExtension(MovieCastPgDao.class, dao -> dao.findCastsByMovieIdAndRole(movieId, role.getId()));
    }

    @Override
    public List<Filmography> findMoviesByStaffId(Integer staffId) {
        return jdbi.withExtension(MovieCastPgDao.class, dao -> dao.findMoviesByStaffId(staffId));
    }

    @Override
    public List<Filmography> findMoviesByStaffIdAndRole(Integer staffId, CastRole role) {
        return jdbi.withExtension(MovieCastPgDao.class, dao -> dao.findMoviesByStaffIdAndRole(staffId, role.getId()));
    }
}
